﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Vine : MonoBehaviour
{
    //获取参数
    #region
    [Header("基础参数")]
    public Collider2D triggerColl;
    public Rigidbody2D playerRb;
    public float hight = 20f;
    public Rigidbody2D anotherPlayerRb;

    [Header("状态")]
    public float storage = 0f;
    public float playerOnNumber = 0f;
    public bool playerIsOn = false;
    public bool anotherPlayerIsOn = false;
    
    bool playerAdd = false;
    bool anotherPlayerAdd = false;
    #endregion
    
    void Start()
    {
        
    }

   
    void Update()
    {
        Shoot();
    }

    //弹射起步
    public void Shoot() 
    {
        if (storage == 2f && playerOnNumber == 1f &&playerIsOn)
        {
            playerAdd = false;
            anotherPlayerAdd = false;
            playerRb.velocity = new Vector2(playerRb.velocity.x, hight);
            storage = 0f;
        }
        if (storage == 2f && playerOnNumber == 1f && anotherPlayerIsOn)
        {
            playerAdd = false;
            anotherPlayerAdd = false;
            anotherPlayerRb.velocity = new Vector2(anotherPlayerRb.velocity.x, hight);
            storage = 0f;

        }
    }
   
    //增加判断（Enter2D）
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            if (playerAdd == true) 
            {
                playerOnNumber = playerOnNumber + 1f;
                playerIsOn = true;
                return;
            }
            playerAdd = true;
            storage = storage+1f;
            playerOnNumber = playerOnNumber + 1f;
            playerIsOn = true;
        }
        
        if (collision.tag == "AnotherPlayer")
        {
            if (anotherPlayerAdd == true)
            {
                playerOnNumber = playerOnNumber + 1f;
                anotherPlayerIsOn = true;
                return;
            }
            anotherPlayerAdd = true;
            storage = storage + 1f;
            playerOnNumber = playerOnNumber + 1f;
            anotherPlayerIsOn = true;
        }
    }
   
    //离开判断（Exit2D）
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            playerOnNumber = playerOnNumber - 1f;
            playerIsOn = false;
        }
        if (collision.tag == "AnotherPlayer")
        {
            playerOnNumber = playerOnNumber - 1f;
            anotherPlayerIsOn = false;
        }
    }

}
